package com.ad.pet.common.structure.ui.control;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.ad.pet.R;


/**
 * Created by z on.
 * Des:圆角textview
 * Date: 2017/4/6
 */

public class CircleTextView extends android.support.v7.widget.AppCompatTextView {

    private Paint mBgPaint;
    private float radius = Resources.getSystem().getDisplayMetrics().density * 4;
    private int defaultColor = getResources().getColor(R.color.colorPrimary);

    public CircleTextView(Context context) {
        this(context, null);
    }

    public CircleTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public CircleTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleTextView);
        defaultColor = ta.getColor(R.styleable.CircleTextView_backgrounColor, defaultColor);
        ta.recycle();
        initParams();
    }

    private void initParams() {
        mBgPaint = new Paint();
        mBgPaint.setColor(defaultColor);
        mBgPaint.setAntiAlias(true);
    }

//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        int measuredWidth = getMeasuredWidth();
//        int measuredHeight = getMeasuredHeight();
//        int max = Math.max(measuredWidth, measuredHeight);
//        setMeasuredDimension(max, max);
//    }

    @Override
    public void setBackgroundColor(@ColorInt int color) {
        mBgPaint.setColor(color);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, Math.max(getWidth(), getHeight()) / 2, mBgPaint);
//        RectF rectF = new RectF(0f, 0f, getMeasuredWidth(), getMeasuredHeight());
//        canvas.drawRoundRect(rectF, radius, radius, mBgPaint);
        super.onDraw(canvas);
    }
}
